package src;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class Manager extends BaseRole {
    public Manager(String userId, String username, String passwordHash, String role, LocalDateTime registrationTime, String phoneNumber) {
        super(userId, username, passwordHash, role, registrationTime, phoneNumber);
    }

    // 实现 BaseRole 的抽象方法：定义经理菜单逻辑
    public void showMenu() {
        Map<Integer, Runnable> options = new HashMap<>();
        options.put(0, null);
        options.put(1, this::viewTodayScreenings);
        options.put(2, this::viewTodaySales);
        options.put(3, this::changeManagerPassword);
        options.put(4, this::resetReceptionistPassword);
        options.put(5, this::listAllMovies);
        options.put(6, this::addMovie);
        options.put(7, this::modifyMovie);
        options.put(8, this::deleteMovie);
        options.put(9, this::queryMovie);
        options.put(10, this::addScreening);
        options.put(11, this::modifyScreening);
        options.put(12, this::deleteScreening);
        options.put(13, this::listAllScreenings);
        options.put(14, this::autoSchedule);
        options.put(15, this::viewTodaySalesData);
        options.put(16, this::viewWeeklySalesData);
        options.put(17, this::viewMonthlySalesData);
        options.put(18, this::manageScreeningRooms);  // 新增放映厅管理选项

        // 调用 BaseRole 的通用菜单循环（标题 + 选项映射）
        menuLoop("经理菜单", options);
    }

    // 实现 BaseRole 的抽象方法：返回选项名称
    @Override
    protected String getOptionName(int choice) {
        switch (choice) {
            case 0: return "返回上一级菜单（退出登录）";
            case 1: return "查看今日放映场次";
            case 2: return "查看今日销售情况";
            case 3: return "修改自身密码";
            case 4: return "重置指定前台用户的密码";
            case 5: return "列出所有正在上映影片的信息";
            case 6: return "添加影片的信息";
            case 7: return "修改电影的信息";
            case 8: return "删除影片的信息";
            case 9: return "查询影片的信息";
            case 10: return "增加场次";
            case 11: return "修改场次";
            case 12: return "删除场次";
            case 13: return "列出所有场次信息";
            case 14: return "自动排片功能";
            case 15: return "查看当日销售数据";
            case 16: return "查看当周销售数据";
            case 17: return "查看当月销售数据";
            case 18: return "放映厅管理";
            default: return "未知选项";
        }
    }

    private void changeManagerPassword() {
        System.out.print("请输入新密码: ");
        String newPassword = Input.getValidStringInput();
        String newPasswordHash = MD5Util.encrypt(newPassword);
        this.setPasswordHash(newPasswordHash);
        
        System.out.println("密码修改成功！");
    }

    private void resetReceptionistPassword() {
        System.out.print("请输入要重置密码的前台用户名: ");
        String targetUsername = Input.getValidStringInput();
        for (User user : CinemaSystem.users) {
            if (user.getUsername().equals(targetUsername) && user.getRole().equals("前台")) {
                String defaultPasswordHash = MD5Util.encrypt("123456");
                user.setPasswordHash(defaultPasswordHash);
                user.setMustChangePassword(true);
                
                LocalDateTime now = LocalDateTime.now();
                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
                Logger.log(now.format(formatter) + " - 操作人: " + this.getUsername() + " - 重置前台" + targetUsername + "的密码");
                System.out.println("用户 " + targetUsername + " 的密码已重置为默认密码 123456，请提醒用户登录后修改。");
                return;
            }
        }
        System.out.println("未找到该前台用户！");
    }

    private void listAllMovies() {
        System.out.println("\n=== 所有正在上映影片的信息 ===");
        for (Movie movie : CinemaSystem.movies) {
            System.out.println(movie);
        }
    }

    private void addMovie() {
        System.out.print("请输入片名: ");
        String title = Input.getValidStringInput();
        System.out.print("请输入导演: ");
        String director = Input.getValidStringInput();
        System.out.print("请输入主演: ");
        String actors = Input.getValidStringInput();
        System.out.print("请输入剧情简介: ");
        String synopsis = Input.getValidStringInput();
        System.out.print("请输入时长（分钟）: ");
        int duration = Input.getValidIntInput(0,10);
        System.out.print("请输入评分（0 - 10分）: ");
        double rating = Input.getValidDoubleInput();

        Movie newMovie = new Movie(title, director, actors, synopsis, duration, rating);
        CinemaSystem.movies.add(newMovie);
        
        LocalDateTime now = LocalDateTime.now();
        Logger.log(now.format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")) + " - 操作人: " + this.getUsername() + " - 增加影片 " + title);
        System.out.println("影片 " + title + " 已添加成功！");
    }

    private void modifyMovie() {
        System.out.print("请输入要修改信息的影片名称: ");
        String targetTitle = Input.getValidStringInput();
        for (Movie movie : CinemaSystem.movies) {
            if (movie.getTitle().equals(targetTitle)) {
                System.out.print("请输入新的片名（不修改请直接回车）: ");
                String newTitle = Input.getValidStringInput();
                if (!newTitle.isEmpty()) {
                    movie.setTitle(newTitle);
                }
                System.out.print("请输入新的导演（不修改请直接回车）: ");
                String newDirector = Input.getValidStringInput();
                if (!newDirector.isEmpty()) {
                    movie.setDirector(newDirector);
                }
                System.out.print("请输入新的主演（不修改请直接回车）: ");
                String newActors = Input.getValidStringInput();
                if (!newActors.isEmpty()) {
                    movie.setActors(newActors);
                }
                System.out.print("请输入新的剧情简介（不修改请直接回车）: ");
                String newSynopsis = Input.getValidStringInput();
                if (!newSynopsis.isEmpty()) {
                    movie.setSynopsis(newSynopsis);
                }
                System.out.print("请输入新的时长（分钟，不修改请直接回车）: ");
                String newDurationStr = Input.getValidStringInput();
                if (!newDurationStr.isEmpty()) {
                    int newDuration = Integer.parseInt(newDurationStr);
                    movie.setDuration(newDuration);
                }
                System.out.print("请输入新的评分（0 - 10分，不修改请直接回车）: ");
                String newRatingStr = Input.getValidStringInput();
                if (!newRatingStr.isEmpty()) {
                    double newRating = Double.parseDouble(newRatingStr);
                    movie.setRating(newRating);
                }
                
                System.out.println("影片 " + targetTitle + " 的信息已修改成功！");
                return;
            }
        }
        System.out.println("未找到该影片！");
    }

    private void deleteMovie() {
        System.out.print("请输入要删除的影片名称: ");
        String targetTitle = Input.getValidStringInput();
        Movie movieToDelete = null;
        for (Movie movie : CinemaSystem.movies) {
            if (movie.getTitle().equals(targetTitle)) {
                movieToDelete = movie;
                break;
            }
        }
        if (movieToDelete == null) {
            System.out.println("未找到该影片！");
            return;
        }
        // 检查是否有排片
        for (Screening screening : CinemaSystem.screenings) {
            if (screening.getMovie().getTitle().equals(targetTitle)) {
                System.out.println("该影片正在上映，已有排片，不允许删除！");
                return;
            }
        }
        System.out.println("警告：删除后无法恢复，是否继续删除操作？(Y/N)");
        String confirm = Input.getValidStringInput();
        if (confirm.equalsIgnoreCase("Y")) {
            CinemaSystem.movies.remove(movieToDelete);
            
            System.out.println("影片 " + targetTitle + " 已删除成功！");
        } else {
            System.out.println("删除操作已取消。");
        }
    }

    private void queryMovie() {
        System.out.println("1. 根据影片名称查询");
        System.out.println("2. 根据导演查询");
        System.out.println("3. 根据主演查询");
        System.out.println("4. 组合查询");
        System.out.print("请选择查询方式: ");
        int choice = Input.getValidIntInput(1,4);
        Input.getValidStringInput(); // 消耗换行符

        switch (choice) {
            case 1:
                System.out.print("请输入影片名称: ");
                String title = Input.getValidStringInput();
                for (Movie movie : CinemaSystem.movies) {
                    if (movie.getTitle().equals(title)) {
                        System.out.println(movie);
                        return;
                    }
                }
                System.out.println("未找到该影片！");
                break;
            case 2:
                System.out.print("请输入导演: ");
                String director = Input.getValidStringInput();
                for (Movie movie : CinemaSystem.movies) {
                    if (movie.getDirector().equals(director)) {
                        System.out.println(movie);
                    }
                }
                break;
            case 3:
                System.out.print("请输入主演: ");
                String actors = Input.getValidStringInput();
                for (Movie movie : CinemaSystem.movies) {
                    if (movie.getActors().contains(actors)) {
                        System.out.println(movie);
                    }
                }
                break;
            case 4:
                System.out.print("请输入影片名称、导演、主演（用空格分隔）: ");
                String[] keywords = Input.getValidStringInput().split(" ");
                for (Movie movie : CinemaSystem.movies) {
                    boolean match = true;
                    for (String keyword : keywords) {
                        if (!movie.getTitle().contains(keyword) && !movie.getDirector().contains(keyword) && !movie.getActors().contains(keyword)) {
                            match = false;
                            break;
                        }
                    }
                    if (match) {
                        System.out.println(movie);
                    }
                }
                break;
            default:
                System.out.println("无效的选择，请重新输入。");
        }
    }

    private void addScreening() {
        System.out.println("可选影片:");
        for (int i = 0; i < CinemaSystem.movies.size(); i++) {
            System.out.println((i + 1) + ". " + CinemaSystem.movies.get(i).getTitle());
        }
        System.out.print("请选择影片序号: ");
        int movieIndex = Input.getValidIntInput(0,Integer.MAX_VALUE) - 1;
        if (movieIndex < 0 || movieIndex >= CinemaSystem.movies.size()) {
            System.out.println("无效的影片序号！");
            return;
        }
        Movie selectedMovie = CinemaSystem.movies.get(movieIndex);

        System.out.println("可选放映厅:");
        for (int i = 0; i < CinemaSystem.rooms.size(); i++) {
            System.out.println((i + 1) + ". " + CinemaSystem.rooms.get(i).getName());
        }
        System.out.print("请选择放映厅序号: ");
        int roomIndex = Input.getValidIntInput(0,Integer.MAX_VALUE) - 1;
        if (roomIndex < 0 || roomIndex >= CinemaSystem.rooms.size()) {
            System.out.println("无效的放映厅序号！");
            return;
        }
        ScreeningRoom selectedRoom = CinemaSystem.rooms.get(roomIndex);

        System.out.print("请输入放映开始时间（格式：yyyy-MM-dd HH:mm）: ");
        LocalDateTime startTime = Input.getValidDateTimeInput();

        // 检查是否提前一周
        LocalDateTime now = LocalDateTime.now();
        if (startTime.isAfter(now.plusDays(7))) {
            System.out.println("不允许提前安排超过一周的电影场次！");
            return;
        }

        System.out.print("请输入票价: ");
        double price = Input.getValidDoubleInput();

        // ==== 新增时间冲突检查 ====
        LocalDateTime endTime = startTime.plusMinutes(selectedMovie.getDuration());
        boolean timeConflict = false;
        String conflictInfo = "";
        
        // 检查当前放映厅是否有时间冲突的场次
        for (Screening existing : CinemaSystem.screenings) {
            if (existing.getRoom().getName().equals(selectedRoom.getName())) {
                LocalDateTime existingEnd = existing.getStartTime().plusMinutes(existing.getMovie().getDuration());
                // 时间重叠判断条件：新场次开始时间早于已有场次结束时间，且新场次结束时间晚于已有场次开始时间
                if (startTime.isBefore(existingEnd) && endTime.isAfter(existing.getStartTime())) {
                    timeConflict = true;
                    conflictInfo = String.format("与现有场次冲突：%s（%s-%s）",
                        existing.getMovie().getTitle(),
                        existing.getStartTime().format(DateTimeFormatter.ofPattern("HH:mm")),
                        existingEnd.format(DateTimeFormatter.ofPattern("HH:mm")));
                    break;
                }
            }
        }
        
        if (timeConflict) {
            System.out.println("无法添加场次：" + conflictInfo);
            return;
        }
        // ==== 冲突检查结束 ====
        
        Screening newScreening = new Screening(selectedMovie, selectedRoom, startTime, price);
        CinemaSystem.screenings.add(newScreening);
        
        System.out.println("场次已添加成功！");
    }

    private void modifyScreening() {
        System.out.println("可选场次:");
        for (int i = 0; i < CinemaSystem.screenings.size(); i++) {
            System.out.println((i + 1) + ". " + CinemaSystem.screenings.get(i));
        }
        System.out.print("请选择要修改的场次序号: ");
        int screeningIndex = Input.getValidIntInput(0,Integer.MAX_VALUE) - 1;
        if (screeningIndex < 0 || screeningIndex >= CinemaSystem.screenings.size()) {
            System.out.println("无效的场次序号！");
            return;
        }
        Screening screeningToModify = CinemaSystem.screenings.get(screeningIndex);

        // 检查该场次的电影票是否有售出
        for (Order order : CinemaSystem.orders) {
            if (order.getScreening().equals(screeningToModify)) {
                System.out.println("该场次的电影票已有售出，不允许修改！");
                return;
            }
        }

        System.out.println("1. 修改影片");
        System.out.println("2. 修改放映厅");
        System.out.println("3. 修改放映时间");
        System.out.println("4. 修改票价");
        System.out.print("请选择要修改的信息: ");
        int choice = Input.getValidIntInput(0,4);
        Input.getValidStringInput(); // 消耗换行符

        switch (choice) {
            case 1:
                System.out.println("可选影片:");
                for (int i = 0; i < CinemaSystem.movies.size(); i++) {
                    System.out.println((i + 1) + ". " + CinemaSystem.movies.get(i).getTitle());
                }
                System.out.print("请选择新的影片序号: ");
                int newMovieIndex = Input.getValidIntInput(0,Integer.MAX_VALUE) - 1;
                if (newMovieIndex < 0 || newMovieIndex >= CinemaSystem.movies.size()) {
                    System.out.println("无效的影片序号！");
                    return;
                }
                Movie newMovie = CinemaSystem.movies.get(newMovieIndex);
                screeningToModify.setMovie(newMovie);
                break;
            case 2:
                System.out.println("可选放映厅:");
                for (int i = 0; i < CinemaSystem.rooms.size(); i++) {
                    System.out.println((i + 1) + ". " + CinemaSystem.rooms.get(i).getName());
                }
                System.out.print("请选择新的放映厅序号: ");
                int newRoomIndex = Input.getValidIntInput(0,Integer.MAX_VALUE) - 1;
                if (newRoomIndex < 0 || newRoomIndex >= CinemaSystem.rooms.size()) {
                    System.out.println("无效的放映厅序号！");
                    return;
                }
                ScreeningRoom newRoom = CinemaSystem.rooms.get(newRoomIndex);
                screeningToModify.setRoom(newRoom);
                break;
            case 3:
                System.out.print("请输入新的放映开始时间（格式：yyyy-MM-dd HH:mm）: ");
                LocalDateTime newStartTime = Input.getValidDateTimeInput();
                
                // ==== 新增时间冲突检查 ====
                LocalDateTime newEndTime = newStartTime.plusMinutes(screeningToModify.getMovie().getDuration());
                boolean timeConflict = false;
                String conflictInfo = "";
                ScreeningRoom currentRoom = screeningToModify.getRoom();;
                
                for (Screening existing : CinemaSystem.screenings) {
                    // 跳过当前正在修改的场次
                    if (System.identityHashCode(existing) == System.identityHashCode(screeningToModify)) {
                        continue;
                    }
                    
                    if (existing.getRoom().getName().equals(currentRoom.getName())) {
                        LocalDateTime existingEnd = existing.getStartTime().plusMinutes(existing.getMovie().getDuration());
                        // 时间重叠判断条件
                        if (newStartTime.isBefore(existingEnd) && newEndTime.isAfter(existing.getStartTime())) {
                            timeConflict = true;
                            conflictInfo = String.format("与现有场次冲突：%s（%s-%s）",
                                existing.getMovie().getTitle(),
                                existing.getStartTime().format(DateTimeFormatter.ofPattern("HH:mm")),
                                existingEnd.format(DateTimeFormatter.ofPattern("HH:mm")));
                            break;
                        }
                    }
                }
                
                if (timeConflict) {
                    System.out.println("无法修改场次时间：" + conflictInfo);
                    return;
                }
                // ==== 冲突检查结束 ====
                
                screeningToModify.setStartTime(newStartTime);
                break;
            case 4:
                System.out.print("请输入新的票价: ");
                double newPrice = Input.getValidDoubleInput();
                screeningToModify.setPrice(newPrice);
                break;
            default:
                System.out.println("无效的选择，请重新输入。");
                return;
        }

        
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Logger.log(now.format(formatter) + " - 操作人: " + this.getUsername() + " - 修改场次的信息：场次时间" + screeningToModify.getStartTime() + "，原片名" + screeningToModify.getMovie().getTitle() + "，修改后的片名" + screeningToModify.getMovie().getTitle());
        System.out.println("场次信息已修改成功！");
    }

    private void deleteScreening() {
        System.out.println("可选场次:");
        for (int i = 0; i < CinemaSystem.screenings.size(); i++) {
            System.out.println((i + 1) + ". " + CinemaSystem.screenings.get(i));
        }
        System.out.print("请选择要删除的场次序号: ");
        int screeningIndex = Input.getValidIntInput(0,Integer.MAX_VALUE) - 1;
        if (screeningIndex < 0 || screeningIndex >= CinemaSystem.screenings.size()) {
            System.out.println("无效的场次序号！");
            return;
        }
        Screening screeningToDelete = CinemaSystem.screenings.get(screeningIndex);

        // 检查该场次的电影票是否有售出
        for (Order order : CinemaSystem.orders) {
            if (order.getScreening().equals(screeningToDelete)) {
                System.out.println("该场次的电影票已有售出，不允许删除！");
                return;
            }
        }

        CinemaSystem.screenings.remove(screeningToDelete);
        
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        Logger.log(now.format(formatter) + " - 操作人: " + this.getUsername() + " - 删除场次的信息：场次时间" + screeningToDelete.getStartTime() + "，片名" + screeningToDelete.getMovie().getTitle());
        System.out.println("场次信息已删除成功！");
    }

    private void listAllScreenings() {
        System.out.println("1. 列出当日所有场次信息");
        System.out.println("2. 列出近3日所有场次信息");
        System.out.println("3. 列出最近一周所有场次信息");
        System.out.print("请选择查看方式: ");
        int choice = Input.getValidIntInput(1,3);

        LocalDateTime now = LocalDateTime.now();
        switch (choice) {
            case 1:
                System.out.println("\n=== 当日所有场次信息 ===");
                for (Screening screening : CinemaSystem.screenings) {
                    if (screening.getStartTime().toLocalDate().equals(now.toLocalDate())) {
                        System.out.println(screening);
                    }
                }
                break;
            case 2:
                System.out.println("\n=== 近3日所有场次信息 ===");
                LocalDateTime threeDaysLater = now.plusDays(3);
                for (Screening screening : CinemaSystem.screenings) {
                    if (screening.getStartTime().isAfter(now) && screening.getStartTime().isBefore(threeDaysLater)) {
                        System.out.println(screening);
                    }
                }
                break;
            case 3:
                System.out.println("\n=== 最近一周所有场次信息 ===");
                LocalDateTime oneWeekLater = now.plusDays(7);
                for (Screening screening : CinemaSystem.screenings) {
                    if (screening.getStartTime().isAfter(now) && screening.getStartTime().isBefore(oneWeekLater)) {
                        System.out.println(screening);
                    }
                }
                break;
            default:
                System.out.println("无效的选择，请重新输入。");
        }
    }

    private void autoSchedule() {
        // 自动排片功能实现
        // 这里可以根据电影评分和黄金时间段进行排片
        // 简单示例：将评分高的电影安排在周末晚上
        LocalDateTime now = LocalDateTime.now();
        for (Movie movie : CinemaSystem.movies) {
            if (movie.getRating() > 8) {
                // 找到周末晚上的时间段
                LocalDateTime startTime = now.withHour(20).withMinute(0);
                while (startTime.getDayOfWeek().getValue() < 6) {
                    startTime = startTime.plusDays(1);
                }
                // 选择一个放映厅
                ScreeningRoom room = CinemaSystem.rooms.get(0);
                double price = 100; // 假设价格为100
                Screening newScreening = new Screening(movie, room, startTime, price);
                CinemaSystem.screenings.add(newScreening);
            }
        }
        
        System.out.println("自动排片完成！");
    }

    private void viewTodaySalesData() {
        int totalSalesCount = 0;
        double totalSalesAmount = 0;
        int totalRefundCount = 0;
        double totalRefundAmount = 0;
        LocalDate today = LocalDate.now();
        System.out.println("\n=== 当日所有场次的实时销售数据 ===");
        for (Order order : CinemaSystem.orders) {
            LocalDateTime orderTime = order.getOrderTime();
            if (orderTime.toLocalDate().equals(today)) {
                totalSalesCount++;
                totalSalesAmount += order.getAmount();
                if (order.isRefunded()) {
                    totalRefundCount++;
                    totalRefundAmount += order.getRefundAmount();
                }
            }
        }
        System.out.println("总销售票数: " + totalSalesCount +
                ", 总销售票数金额: ￥" + totalSalesAmount +
                ", 总退票数: " + totalRefundCount +
                ", 退票金额: ￥" + totalRefundAmount +
                ", 总票价: ￥" + (totalSalesAmount - totalRefundAmount));

        }

    private void viewWeeklySalesData() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneWeekLater = now.plusDays(7);
        int totalSalesCount = 0;
        double totalSalesAmount = 0;
        int totalRefundCount = 0;
        double totalRefundAmount = 0;
        System.out.println("\n=== 当周所有场次的统计销售数据 ===");
        for (Order order : CinemaSystem.orders) {
            if (order.getOrderTime().isAfter(now) && order.getOrderTime().isBefore(oneWeekLater)) {
                totalSalesCount++;
                totalSalesAmount += order.getAmount();
                if (order.isRefunded()) {
                    totalRefundCount++;
                    totalRefundAmount += order.getRefundAmount();
                }
            }
        }
        System.out.println("总销售票数: " + totalSalesCount +
                ", 总销售票数金额: ￥" + totalSalesAmount +
                ", 总退票数: " + totalRefundCount +
                ", 退票金额: ￥" + totalRefundAmount +
                ", 总票价: ￥" + (totalSalesAmount - totalRefundAmount));
    }

    private void viewMonthlySalesData() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime oneMonthLater = now.plusMonths(1);
        int totalSalesCount = 0;
        double totalSalesAmount = 0;
        int totalRefundCount = 0;
        double totalRefundAmount = 0;
        System.out.println("\n=== 当月所有场次的统计销售数据 ===");
        for (Order order : CinemaSystem.orders) {
            if (order.getOrderTime().isAfter(now) && order.getOrderTime().isBefore(oneMonthLater)) {
                totalSalesCount++;
                totalSalesAmount += order.getAmount();
                if (order.isRefunded()) {
                    totalRefundCount++;
                    totalRefundAmount += order.getRefundAmount();
                }
            }
        }
        System.out.println("总销售票数: " + totalSalesCount +
                ", 总销售票数金额: ￥" + totalSalesAmount +
                ", 总退票数: " + totalRefundCount +
                ", 退票金额: ￥" + totalRefundAmount +
                ", 总票价: ￥" + (totalSalesAmount - totalRefundAmount));
    }

    private void viewTodayScreenings() {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("\n=== 今日放映场次 ===");
        for (Screening screening : CinemaSystem.screenings) {
            if (screening.getStartTime().toLocalDate().equals(now.toLocalDate())) {
                System.out.println(screening);
            }
        }
    }

    private void viewTodaySales() {
        LocalDateTime now = LocalDateTime.now();
        double totalSales = 0;
        System.out.println("\n=== 今日销售情况 ===");
        for (Order order : CinemaSystem.orders) {
            if (order.getOrderTime().toLocalDate().equals(now.toLocalDate())) {
                totalSales += order.getAmount();
                System.out.println(order);
            }
        }
        System.out.println("今日总销售额: ￥" + totalSales);
    }


// 新增放映厅管理方法
private void manageScreeningRooms() {
    System.out.println("\n=== 放映厅管理 ===");
    System.out.println("1. 查看所有放映厅");
    System.out.println("2. 添加新放映厅");
    System.out.println("3. 修改放映厅");
    System.out.println("4. 删除放映厅");
    int choice = Input.getValidIntInput(1, 4);

    switch (choice) {
        case 1: listAllScreeningRooms(); break;
        case 2: addScreeningRoom(); break;
        case 3: modifyScreeningRoom(); break;
        case 4: deleteScreeningRoom(); break;
    }
}

    private void listAllScreeningRooms() {
        System.out.println("\n=== 所有放映厅 ===");
        for (ScreeningRoom room : CinemaSystem.rooms) {
            System.out.printf("名称: %s  行数: %d  每行座位数: %d\n", 
                room.getName(), room.getRows(), room.getSeatsPerRow());
        }
    }
    
    private void addScreeningRoom() {
        System.out.print("请输入放映厅名称: ");
        String name = Input.getValidStringInput();
        System.out.print("请输入行数: ");
        int rows = Input.getValidIntInput(1, 100);
        System.out.print("请输入每行座位数: ");
        int seatsPerRow = Input.getValidIntInput(1, 100);
        CinemaSystem.rooms.add(new ScreeningRoom(name, rows, seatsPerRow));
        System.out.println("放映厅添加成功！");
    }
    
    private void modifyScreeningRoom() {
        System.out.print("请输入要修改的放映厅名称: ");
        String target = Input.getValidStringInput();
        for (ScreeningRoom room : CinemaSystem.rooms) {
            if (room.getName().equals(target)) {
                System.out.print("新行数（留空不修改）: ");
                String newRows = Input.getValidStringInput();
                if (!newRows.isEmpty()) room = new ScreeningRoom(room.getName(), Integer.parseInt(newRows), room.getSeatsPerRow());
                
                System.out.print("新每行座位数（留空不修改）: ");
                String newSeats = Input.getValidStringInput();
                if (!newSeats.isEmpty()) room = new ScreeningRoom(room.getName(), room.getRows(), Integer.parseInt(newSeats));
                
                System.out.println("放映厅信息修改成功！");
                return;
            }
        }
        System.out.println("未找到该放映厅！");
    }
    
    private void deleteScreeningRoom() {
        System.out.print("请输入要删除的放映厅名称: ");
        String target = Input.getValidStringInput();
        boolean hasScreenings = CinemaSystem.screenings.stream()
            .anyMatch(s -> s.getRoom().getName().equals(target));
        if (hasScreenings) {
            System.out.println("该放映厅有未结束的场次，无法删除！");
            return;
        }
        boolean removed = CinemaSystem.rooms.removeIf(r -> r.getName().equals(target));
        System.out.println(removed ? "删除成功！" : "未找到该放映厅！");
    }
}